<template>
  <div :style="{height:'410px',width:width}">
    <el-row >
    <el-col :span="5">
       
    <!-- <span class="demonstration">月</span> -->
    <el-date-picker
      v-model="queryParams.month"
      type="month"
      value-format="yyyy-M"
      placeholder="选择月"
      @change="queryData"
      >
      
    </el-date-picker>
  
  </el-col>


</el-row>
<el-row>
  <div :class="className" :style="{height:height,width:width}" ref="box" />
</el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getMonthSalesData } from "@/api/index";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      queryParams: {
        month: '2024-5',
        year:'2024'
      },
      seriesData:[],
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
     this.queryData()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  methods: {
    queryData(){
      console.log(this.queryParams.month)
     var dds= this.queryParams.month.split("-");
     var year=dds[0];
     var month=dds[1];
     console.log(this.queryParams)
      getMonthSalesData(year,month).then(
        response=>{
       var data =response.data['seriesData'];
        if(data.length>0){
            var dd=[];
            
            dd.push({ value: data[0]['saleAmount'], name: '销售数据' })
            dd.push({ value: data[0]['saleCostAmount'], name: '成本数据' })
            this.seriesData=dd;
          
          }
          else
          {
            var dd=[];
            
            dd.push({ value:0, name: '销售数据' })
            dd.push({ value: 0, name: '成本数据' })
            this.seriesData=dd;
          }
          this.initChart();
        }
      )

    },
    initChart() {
      this.chart = echarts.init(this.$refs.box,'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['销售数据', '成本数据']
        },
        series: [
          {
            name: '销售成本分析',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.seriesData,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
